package com.plwf.lake.controller;

import com.plwf.lake.entity.LakeAdmin;
import com.plwf.lake.service.LakeAdminService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 管理员接口
 * @author ：plwf
 * @time：2019-5-12
 */
@RestController
@RequestMapping("/admin")
public class LakeAdminCtrl {
    @Resource
    private LakeAdminService service;

    //登陆
    @RequestMapping(value ="/login",method= RequestMethod.POST)
    public LakeAdmin login(String username, String password){
        LakeAdmin bean = new LakeAdmin();
        bean.setUsername(username);
        bean.setPassword(password);
        bean = service.login(bean);
        return bean;
    }

   //通过id查找管理员信息，包含管理员的权限
    @RequestMapping("/find/{id}")
    public List<LakeAdmin> find(@PathVariable("id") Integer id){
        return service.find(id);
    }

    //未用
    @RequestMapping("/query")
    public List<LakeAdmin> query(){
        return service.query();
    }

    //保存管理员信息
    @RequestMapping("/save")
    public boolean save(HttpServletRequest request) throws SQLException {
        String id = request.getParameter("id");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String status = request.getParameter("status");
        String ids = request.getParameter("ids");

        List<Integer> list = new ArrayList<>();
        if(ids!=null && ids.length()>0){
            String[] pids = ids.split(",");
            for(String s : pids){
                list.add(Integer.parseInt(s));
            }
        }

        LakeAdmin bean = new LakeAdmin();
        bean.setUsername(username);
        bean.setPassword(password);
        bean.setStatus(status.toString());
        if(id!=null && !"".equals(id)){
            bean.setId(Integer.parseInt(id));
        }

        return service.save(bean, list);
    }

    //删除管理员
    @RequestMapping("/remove/{classId}")
    public boolean remove(@PathVariable("classId") Integer classId){
        return service.remove(classId);
    }

}
